//
//  OpMineController.m
//  LoveFreshBeeReplica
//
//  Created by skywalker on 16/12/30.
//  Copyright © 2016年 斯芬克斯. All rights reserved.
//

#import "OpMineController.h"
#import "OpNextView.h"
#import "NetworkTool.h"
#import "OpAddRessView.h"
#import "OpShopView.h"
#import "OpHelpView.h"
#import "OpIdeaView.h"
#import "MyAddressTableViewCell.h"
#import "SettingViewController.h"


@interface OpMineController ()
@property(nonatomic,strong) NSMutableArray *arr;
@end
static NSString *nextViewID = @"nextViewID";
static NSString *otherCellID = @"otherCellID";
@implementation OpMineController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 150)];
    UIImageView *iv = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"v2_my_avatar"]];
    [headerView addSubview:iv];
    UILabel *versonLabel = [[UILabel alloc]init];
    versonLabel.text = @"18635629040";
    [headerView addSubview:versonLabel];
    
    UIButton *setBtn = [[UIButton alloc]init];
    [setBtn setImage:[UIImage imageNamed:@"v2_my_settings_icon"] forState:UIControlStateNormal];
    
    [headerView addSubview:setBtn];
    
    [setBtn addTarget:self action:@selector(BtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    
    [iv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(headerView).offset(10);
        make.centerX.equalTo(headerView);
    }];
    [versonLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(headerView).offset(-10);
        make.centerX.equalTo(headerView);
    }];
    [setBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(headerView).offset(10);
        make.right.equalTo(headerView).offset(-10);
    }];
    headerView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"v2_my_avatar_bg"]];
    self.tableView.tableHeaderView = headerView;
    [self.tableView registerNib:[UINib nibWithNibName:@"OpNextView" bundle:nil] forCellReuseIdentifier:nextViewID];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:otherCellID];
    self.tableView.separatorInset = UIEdgeInsetsZero;
//    [self POST];
    self.tableView.tableFooterView = [UIView new];
    
           
    
    
    
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 4;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0 || section == 2) {
        return 1;
    }else{
        return 2;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0)
    {
        OpNextView *nextCell = [tableView dequeueReusableCellWithIdentifier:nextViewID forIndexPath:indexPath];
        nextCell.selectionStyle = UITableViewCellSelectionStyleNone;
        return nextCell;
    }
    else
    {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:otherCellID forIndexPath:indexPath];
        if(indexPath.section == 1 && indexPath.row == 0)
        {
            UIImage *image = [UIImage imageNamed:@"v2_my_address_icon"];
            UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
            [cell.contentView addSubview:imageView];
            UILabel *mineLabel = [[UILabel alloc]init];
            [cell.contentView addSubview:mineLabel];
            mineLabel.text = @"我的收获地址";
            [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(cell.mas_centerY).offset(0);
                make.left.equalTo(self.tableView.mas_right).offset(10);
            }];
            [mineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(cell.mas_centerY).offset(0);
                make.left.equalTo(imageView.mas_right).offset(10);
            }];
        }
        if(indexPath.section == 1 && indexPath.row == 1)
        {
            UIImage *image = [UIImage imageNamed:@"icon_mystore"];
            UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
            [cell.contentView addSubview:imageView];
            UILabel *mineLabel = [[UILabel alloc]init];
            [cell.contentView addSubview:mineLabel];
            mineLabel.text = @"我的店铺";
            [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(cell.mas_centerY).offset(0);
                make.left.equalTo(self.tableView.mas_right).offset(10);
            }];
            [mineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(cell.mas_centerY).offset(0);
                make.left.equalTo(imageView.mas_right).offset(10);
            }];
        }
        if(indexPath.section == 2)
        {
            UIImage *image = [UIImage imageNamed:@"v2_my_share_icon"];
            UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
            [cell.contentView addSubview:imageView];
            UILabel *mineLabel = [[UILabel alloc]init];
            [cell.contentView addSubview:mineLabel];
            mineLabel.text = @"把爱鲜蜂分享给别人";
            [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(cell.mas_centerY).offset(0);
                make.left.equalTo(self.tableView.mas_right).offset(10);
            }];
            [mineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(cell.mas_centerY).offset(0);
                make.left.equalTo(imageView.mas_right).offset(10);
            }];
        }
        if(indexPath.section == 3 && indexPath.row == 0)
        {
            UIImage *image = [UIImage imageNamed:@"v2_my_serviceonline_icon"];
            UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
            [cell.contentView addSubview:imageView];
            UILabel *mineLabel = [[UILabel alloc]init];
            [cell.contentView addSubview:mineLabel];
            mineLabel.text = @"客服帮助";
            [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(cell.mas_centerY).offset(0);
                make.left.equalTo(self.tableView.mas_right).offset(10);
            }];
            [mineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(cell.mas_centerY).offset(0);
                make.left.equalTo(imageView.mas_right).offset(10);
            }];
        }
        if(indexPath.section == 3 && indexPath.row == 1)
        {
            UIImage *image = [UIImage imageNamed:@"v2_my_feedback_icon"];
            UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
            [cell.contentView addSubview:imageView];
            UILabel *mineLabel = [[UILabel alloc]init];
            [cell.contentView addSubview:mineLabel];
            mineLabel.text = @"意见反馈";
            [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(cell.mas_centerY).offset(0);
                make.left.equalTo(self.tableView.mas_right).offset(10);
            }];
            [mineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(cell.mas_centerY).offset(0);
                make.left.equalTo(imageView.mas_right).offset(10);
            }];
        }
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        return cell;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0)
    {
        return 100;
    }
    return 40;
}

- (CGFloat )tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 0.01;
    }else{
        return 10;
    }
}
/*
-(NSDictionary *)POST
{
    //http://iosapi.itcast.cn/loveBeen/MyOrders.json.php
    NSDictionary *parameters = [NSDictionary dictionaryWithObject:@"13" forKey:@"call"];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html" , @"text/plain", nil];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    
    [manager POST:@"http://iosapi.itcast.cn/loveBeen/MyOrders.json.php" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSString *str = [[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSLog(@"%@",str);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"error %@",error);
    }];
    return nil;
}
*/
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 1 && indexPath.row == 0)
    {
        OpAddRessView *vc = [[OpAddRessView alloc]init];
        vc.title = @"收货地址";
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
    if(indexPath.section == 1 && indexPath.row == 1)
    {
        OpShopView *vc = [[OpShopView alloc]init];
        vc.title = @"我的店铺";
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
    if(indexPath.section == 3 && indexPath.row == 0)
    {
        OpHelpView *vc = [[OpHelpView alloc]init];
        vc.title = @"客服帮助";
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
    if(indexPath.section == 3 && indexPath.row == 1)
    {
        OpIdeaView *vc = [[OpIdeaView alloc]init];
        vc.title = @"意见反馈";
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
    if(indexPath.section == 2)
    {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"分享" message:nil preferredStyle: UIAlertControllerStyleActionSheet];
        UIAlertAction *QXAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        UIAlertAction *WXAction = [UIAlertAction actionWithTitle:@"微信" style:UIAlertActionStyleDefault handler:nil];
        UIAlertAction *WBAction = [UIAlertAction actionWithTitle:@"微博" style:UIAlertActionStyleDefault handler:nil];
        UIAlertAction *QQAction = [UIAlertAction actionWithTitle:@"QQ" style:UIAlertActionStyleDefault handler:nil];
        [alertController addAction:QXAction];
        [alertController addAction:WXAction];
        [alertController addAction:WBAction];
        [alertController addAction:QQAction];
        [self presentViewController:alertController animated:YES completion:nil];
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)BtnClick:(UIButton *)btn {
    

    SettingViewController * vc = [[SettingViewController alloc] init];
    vc.title = @"设置";
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
